// MarkFormView.cpp : implementation file
//

#include "stdafx.h"
#include "codehelp.h"
#include "MarkFormView.h"
#include "CHAdo.h"
#include "CHTreeCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMarkFormView

IMPLEMENT_DYNCREATE(CMarkFormView, CFormView)

CMarkFormView::CMarkFormView()
	: CFormView(CMarkFormView::IDD)
{
	m_hBrush = NULL;

	//{{AFX_DATA_INIT(CMarkFormView)
	//}}AFX_DATA_INIT
}

CMarkFormView::~CMarkFormView()
{
}

void CMarkFormView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMarkFormView)
	DDX_Control(pDX, IDC_BTN_REFRESH, m_refresh);
	DDX_Control(pDX, IDC_BTN_DELETE, m_delete);
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMarkFormView, CFormView)
	//{{AFX_MSG_MAP(CMarkFormView)
	ON_WM_CTLCOLOR()
	ON_WM_SIZE()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_BTN_DELETE, OnBtnDelete)
	ON_BN_CLICKED(IDC_BTN_REFRESH, OnBtnRefresh)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMarkFormView diagnostics

#ifdef _DEBUG
void CMarkFormView::AssertValid() const
{
	CFormView::AssertValid();
}

void CMarkFormView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMarkFormView message handlers

HBRUSH CMarkFormView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	UNREFERENCED_PARAMETER(pWnd);
	UNREFERENCED_PARAMETER(nCtlColor);
	
	if (m_hBrush != NULL)
		::DeleteObject(m_hBrush);

	m_hBrush = ::CreateSolidBrush(RGB(255,255,255));

	pDC->SetBkColor(RGB(255, 255, 255));
	pDC->SetTextColor(RGB(0,0,0));

	return m_hBrush;
}

void CMarkFormView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	m_delete.SetBitmaps(IDB_DELETE, RGB(255, 255, 255));
	m_delete.SetTooltipText("CMarkFormView tooltip text1");
	m_delete.DrawTransparent(FALSE);
	m_delete.SetFlat(TRUE);

	m_refresh.SetBitmaps(IDB_REFRESH, RGB(255, 255, 255));
	m_refresh.DrawTransparent(FALSE);
	m_refresh.SetTooltipText("CMarkFormView tooltip text2");
	m_refresh.SetFlat(TRUE);

	m_list.SubclassHeader();
	m_imageList.Create(1, 17, ILC_COLOR24|ILC_MASK, 2, 2);
	m_list.SetImageList(&m_imageList, LVSIL_SMALL);
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
	m_list.AddColumn("CMarkFormView column1", 160);

	LoadMark();
}

void CMarkFormView::OnSize(UINT nType, int cx, int cy) 
{
	if( m_list.GetSafeHwnd())
	{
		const int nMarginX = 4;
		const int nMarginY = 4;
		const int nBtnWidth = 80;
		const int nBtnHeight = 18;

		CRect rc;

		GetClientRect(&rc);
		ClientToScreen(&rc);

		if(rc.Height() < 100)
		{
			m_list.ShowWindow(FALSE);
		}
		else
		{
			m_list.ShowWindow(TRUE);
			m_list.MoveWindow(nMarginX, 
							  nMarginY + nBtnHeight,
							  rc.Width() - nMarginX * 2,
							  rc.Height() - nMarginY* 2 - nBtnHeight);
			m_list.SetColumnWidth(0, rc.Width() - nMarginX * 2 - 8);
		}

		m_delete.MoveWindow(rc.Width() - nBtnWidth - nMarginX*2 + 3, 
				nMarginY, nBtnWidth, nBtnHeight);
		m_refresh.MoveWindow(rc.Width() - nBtnWidth*2 - nMarginX*3 + 3, 
				nMarginY, nBtnWidth, nBtnHeight);
	}
}

void CMarkFormView::LoadMark()
{
	if(!theApp.conn->State)
		return;

	m_list.DeleteAllItems();
	CHAdo ado(theApp.conn, theApp.rset);
	try
	{
		ado.Select("SELECT NodeId, Title FROM TTree WHERE NOT (IsNull(MarkTime) OR MarkTime = 0) AND [Type] = 1 ORDER BY NodeId");
		while(!ado.GetRecordset()->adoEOF)
		{
			m_list.SetItemData(
				m_list.InsertItem(m_list.GetItemCount(), ado.GetText(1)),
				ado.GetLong(0));
			ado.GetRecordset()->MoveNext();
		}
	}
	catch(_com_error& e)
	{
		ado.ShowError(e);
	}
}

void CMarkFormView::RemoveMark()
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos != NULL)
	{
		int nItem = m_list.GetNextSelectedItem(pos);
		int nNodeId = m_list.GetItemData(nItem);

		CString str;
		CHAdo ado(theApp.conn, theApp.rset);
		try
		{
			str.Format("UPDATE TTree SET MarkTime = 0 WHERE NodeId=%d", nNodeId);
			ado.Execute(str);
			m_list.DeleteItem(nItem);
		}
		catch(_com_error& e)
		{
			ado.ShowError(e);
		}
	}
}

void CMarkFormView::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos != NULL)
	{
		int nItem = m_list.GetNextSelectedItem(pos);
		int nNodeId = m_list.GetItemData(nItem);
		theQueue.RemoveAll();
		theQueue.AddTail(nNodeId);

		CString str;
		CHAdo ado(theApp.conn, theApp.rset);
		try
		{
			while(true)
			{
				str.Format("SELECT ParentId FROM TTree WHERE NodeId=%d", nNodeId);
				ado.Select(str);
				if(ado.GetRecordset()->adoEOF) return;
				nNodeId = ado.GetLong(0);

				if(nNodeId != 0)
					theQueue.AddTail(nNodeId);
				else
					break;
			}
			NMHDR nm;
			nm.hwndFrom = GetSafeHwnd();
			nm.idFrom = 0;
			nm.code = CH_TREE_FIND;
			theApp.m_pMainWnd->SendMessage(WM_NOTIFY, 0, (LPARAM)&nm);
		}
		catch(_com_error& e)
		{
			ado.ShowError(e);
		}
	}

	*pResult = 0;
}

BOOL CMarkFormView::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_SYSCHAR &&(pMsg->wParam == 'R' || pMsg->wParam == 'r'))
	{
		LoadMark();
		return TRUE;
	}

	if(pMsg->message == WM_SYSCHAR &&(pMsg->wParam == 'D' || pMsg->wParam == 'd'))
	{
		RemoveMark();
		return TRUE;
	}

	return CFormView::PreTranslateMessage(pMsg);
}


void CMarkFormView::OnBtnDelete() 
{
	RemoveMark();
}

void CMarkFormView::OnBtnRefresh() 
{
	LoadMark();
}
